
cc.Class({
   extends: cc.Component,

   properties: {
      enemySprites: [cc.SpriteFrame],
      enemyProgessBar: cc.ProgressBar,
   },



   onLoad() {
      //获取子弹管理者
      this.bulletMgrNode = cc.find("Canvas/bulletMgr");
   },

   start() {

   },

   update(dt) { },

   //碰撞回调函数，敌机与玩家或者宠物发生碰撞，都需要移除
   onCollisionEnter: function (other, self) {
      if (other.node.group === "player") {
         other.node.getComponent("Player").playerHurt(true);
         this.node.removeFromParent();
      }
   },
   //获取敌机在json中的数据
   initWithDt(enemyDt) {
      //不能直接将enemyDt传递给某个变量，因为里面的数据是引用类型，如果改变被赋值的变量的值，
      // 会将json中的数据也会改变
      //获取敌机的名字
      this.imgName = enemyDt.img;
      //获取敌机的血量
      this.hp = enemyDt.hp;
      this.maxHp = enemyDt.hp;
      //获取敌机的分数
      this.score = enemyDt.score;
      //设置敌机的精灵帧
      for (let img of this.enemySprites) {
         if (img.name == this.imgName) {
            this.node.getComponent(cc.Sprite).spriteFrame = img;
         }
      }
   },
   //控制敌机移动
   enemyMove() {
      cc.tween(this.node)
         .by(2, { position: cc.v2(0, -cc.winSize.height - this.node.height) })
         .removeSelf()
         .start()
      //敌机发射子弹
      this.schedule(this.enemyFire, 0.5);
   },

   //敌机受伤进行扣血
   hurt(type) {
      if (type === "pet") {
         this.hp -= 0.5;
      }
      else if (type !== "pet") {
         this.hp -= 1;
      }

      this.enemyProgessBar.progress = this.hp / this.maxHp;
      if (this.hp <= 0) {
         this.node.removeFromParent();
         //敌机被歼灭，获取分数节点,调用增加分数的逻辑
         let scoreNode = cc.find("Canvas/score");
         let scoreNodeJs = scoreNode.getComponent("Score");
         scoreNodeJs.addScore(this.score);
      }
   },
   //敌机发射子弹
   enemyFire() {
      let bulletMgrJs = this.bulletMgrNode.getComponent("BulletMgr");
      bulletMgrJs.createBullet(this.node.position, "enemy", this.imgName);
   },


});
